package com.zz.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zz.common.Log;
import com.zz.common.R;
import com.zz.service.LoggingEventService;
import com.zz.service.SystemLogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Nullable;
import javax.validation.constraints.NotNull;
import java.text.ParseException;

/**
 * 日志审计
 * @author 朱彦燕
 */
@RestController
@Slf4j
@RequestMapping("/log")
@CrossOrigin
@Validated
public class LogController {

    @Autowired
    private SystemLogService systemLogService;

    @Autowired
    private LoggingEventService loggingEventService;

    /**
     * 用户操作日志分页查询
     *
     * @param page  起始页码
     * @param pageSize  每页数量
     * @param startTime 起始时间（允许为空）
     * @param endTime 结束时间（允许为空）cmd
     * @return
     */
    @RequiresRoles("admin")
    @GetMapping("/sLog")
    @Log(title = "查看操作日志", dataLocationType = "link")
    public R<Page> sLog(@RequestParam @NotNull(message = "起始页码不能为空") int page,
                        @RequestParam @NotNull(message = "每页最大值不能为空") int pageSize,
                        @RequestParam @Nullable String startTime, @RequestParam @Nullable String endTime){
        return systemLogService.sLog(page, pageSize, startTime, endTime);
    }

    /**
     * slf4j框架日志logging_event分页查询
     * @param page  起始页码
     * @param pageSize  每页数量
     * @param startTime 起始时间（允许为空）
     * @param endTime 结束时间（允许为空）
     * @return
     */
    @RequiresRoles("admin")
    @GetMapping("/logEvent")
    @Log(title = "slf4j框架日志logging_event分页查询", dataLocationType = "link")
    public R<Page> logEvent(@RequestParam @NotNull(message = "起始页码不能为空") int page,
                            @RequestParam @NotNull(message = "每页最大值不能为空") int pageSize,
                            @RequestParam @Nullable String startTime, @RequestParam @Nullable String endTime) throws ParseException {
        return loggingEventService.logEvent(page, pageSize, startTime, endTime);
    }

}
